//
//  CollectViewController.m
//  减肥,美容老中医偏方
//
//  Created by appying_wxj on 14/12/4.
//  Copyright (c) 2014年 陈朕. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectDetailViewController.h"
@interface CollectViewController ()

@end

@implementation CollectViewController
@synthesize cell,array;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, boundswidth-120, 44)];
        navLabel.text = @"我的收藏";
        navLabel.textColor = [UIColor colorWithRed:1 green:0.94 blue:0.82 alpha:1];
        navLabel.font = [UIFont systemFontOfSize:18];
        navLabel.textAlignment = NSTextAlignmentCenter;
        self.navigationItem.titleView = navLabel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:0.68 green:0.45 blue:0.2 alpha:1];
    [self initCollectUI];
    [_collectTableView reloadData];
}
-(void)viewDidAppear:(BOOL)animated{
    array = [BaseAllCase getAllCaseTitle];
    
    [_collectTableView reloadData];

}
// 搭建收藏的界面
-(void)initCollectUI{
    _collectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, boundswidth, boundsheight-10)];
    _collectTableView.backgroundColor = [UIColor clearColor];
    _collectTableView.delegate = self;
    _collectTableView.dataSource = self;
    [_collectTableView registerClass:[AppTableViewCell class] forCellReuseIdentifier:@"TableSampleIdentifierCell"];
    _collectTableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 去掉表中的分割线
    [self.view addSubview:_collectTableView];
}
// 获取数据库中的数据
-(void)getCollectDate{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifierCell";
    cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        
        cell = [[AppTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
    }
    else{  //重要，释放cell,防止闪退方法，其他CustomCell 均可沿用
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    
    AllCase *p = [array objectAtIndex:indexPath.row];
    
    cell.title.frame = CGRectMake(20, 2, 260, 40);
    cell.title.text = [NSString stringWithFormat:@"%@",p.caseTitle];
    
    return cell;
}
//点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    dispatch_async(dispatch_get_main_queue(), ^
                   {
                       
                       appBean = [AppBean shareInstence];
                       [appBean setTitle:[[array objectAtIndex:indexPath.row] caseTitle]];
                       [appBean setUrl:[[array objectAtIndex:indexPath.row] caseContent]];
                       
                       CollectDetailViewController *content = [[CollectDetailViewController alloc] init];
                       [self.navigationController pushViewController:content animated:YES];
                       
                   });
}
// 编辑单元格  删除数据库中的数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 1,根据索引从数组中找到要删除的对象
    AllCase *p = [array objectAtIndex:indexPath.row];
    
    [BaseAllCase deleteFruits:p.caseTitle];
    
    // 3.删除数组中的对象
    [array removeObjectAtIndex:indexPath.row];
    
    // 4.删除行
    [_collectTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
    self.array = [BaseAllCase getAllCaseTitle];
    [_collectTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
